export const tokenKey = 'Authorization'

let jwtToken = '';
export function getAuthorization() {
	if (!jwtToken) {
		jwtToken = uni.getStorageSync(tokenKey) || '';
	}
	return jwtToken;
}

export function setAuthorization(token) {
	uni.setStorageSync(tokenKey, jwtToken = token)
}

export function deleteAuthorization() {
	jwtToken = '';
	uni.removeStorageSync(tokenKey);
}

export function hasAuth() {
	return !!getAuthorization();
}

export function trimParams(params) {
	if (params && typeof params === 'object') {
		Object.keys(params).forEach(key => {
			const value = params[key];
			if (value === null) {
				params[key] = '';
			} else if (typeof value === 'string') {
				params[key] = value.trim();
			}
		});
	}
	return params;
}

export function url2obj(url) {
	const arr = url.split('&');
	const rt = {};
	arr.forEach(item => {
		const kv = item.split('=', 2);
		if (kv.length > 1) {
			rt[kv[0]] = decodeURIComponent(kv[1]);
			if (!isNaN(rt[kv[0]])) {
				rt[kv[0]] = parseFloat(rt[kv[0]]);
			}
		}
	});
	return rt;
}

export function showLoading(msg) {
	uni.showLoading({
		title: msg || '加载中...'
	})
};

export function hideLoading() {
	uni.hideLoading()
}

export function toast(msg) {
	uni.showToast({
		icon: 'none',
		title: msg
	})
}
